app = {} //The app table is deleted later, so store data in app.app._ = {} instead. Not using the table app.app._ in specific will cause problems.
app.folder = {"System"} //This table should have one key and one value, key of one, value of a string. This is for FUTURE subfolder support.
app.app = {
	caption = "System Scheme",
	options = {
			close = true,
			minimize = true,
			resize = false,
		},
	x = ScrW()*.5 - 128 + 256 + 16,
	y = ScrH()*.5 - 64,
	w = 256,
	h = 64+4+(12*20),
	_ = {
		cur = "c1",
		curlabel = "GUI Inactive",
		save = hack.scheme.c1
	},
	draw = function(self)
		if not self.visible then
			return
		end
		local s = hack.scheme
		self:DrawWindowPre()
		
		local c = s[self._.cur]
		surface.SetDrawColor(c.r,c.g,c.b,c.a)
		self:DrawRect(4,4,16,16)
		self:DrawText(self._.curlabel,"Trebuchet18",26,2)
		
		//Selector
		if hack.focus[1] == self.key and self.mx >= 4 and self.mx <= self.w - 4 and self.my >= 68 and self.my <= 66+12*20 then
			//self:DrawRect(4,66,self.w-8,12*20,Color(255,0,0,255))
			local i = math.Clamp(math.floor((self.my - 68)/20),0,#hack.schemelabel-1)
			local yo = (i)*20+4+64
			if self.mc[0].click == 2 and self.mc[0].uid == self.uid and self.mc[0].data.act == "sel" and self.mc[0].data.sel == i+1 then
				self:DrawRect(24,yo,self.w-28,16,s.c4)
			else
				self:DrawRect(24,yo,self.w-28,16,s.c3)
			end
			
			if self.mc[0].click == 1 then
				self.mc[0] = {
					click = 2,
					uid = self.uid, //ALWAYS assign self.uid
					data = {
						act = "sel",
						sel = i+1
					}
				}
			end
		end
		
		if self.mc[0].click == -1 and self.mc[0].uid == self.uid and self.mc[0].data.act == "sel" then //Exit click function
			//ALWAYS perform click actions on release
			
			if hack.focus[1] == self.key and self.mx >= 4 and self.mx <= self.w - 4 and self.my >= 68 and self.my <= 66+12*20 then
				local i = math.Clamp(math.floor((self.my - 68)/20),0,#hack.schemelabel-1)+1
				
				if i == self.mc[0].data.sel then
					self._.cur = hack.schemelabel[i].key
					self._.curlabel = hack.schemelabel[i].label
					self._.save = Color(0,0,0,0)
					for k,v in pairs({"r","g","b"}) do
						self._.save[v] = s[self._.cur][v]
					end
				end
			end
			
			self.mc[0] = { //ALWAYS clear the click table when finished
				click=0,
				uid=nil,
				data=nil,
			}
		end
		
		//selDisplay
		local i = 1
		for k,v in pairs(hack.schemelabel) do
			local yo = (i-1)*20+4+64
			
			local c = hack.scheme[v.key]
			surface.SetDrawColor(c.r,c.g,c.b,c.a)
			self:DrawRect(4,yo,16,16)
			self:DrawText(v.label,"Trebuchet18",24,yo)
			i = i + 1
		end
		
		//Color chooser
		local i = 1
		for k,v in pairs({"r","g","b"}) do
			yo = (i)*14+10
			
			self:DrawRect(4,yo,132,12,s.c2)
			
			surface.SetDrawColor(
				v == "r" and 255 or 0,
				v == "g" and 255 or 0,
				v == "b" and 255 or 0,
				255
			)
			
			local new = nil
			
			if self.mc[0].click == 2 and self.mc[0].uid == self.uid and self.mc[0].data.col == v then //Drag function
				new = math.Round(math.Clamp((self.mx - 4)*2-1,0,255))
			elseif hack.focus[1] == self.key and self.mx >= 4 and self.mx <= 136 and self.my >= yo and self.my <= yo+12 then //Enter click function
				//ALWAYS make sure we have the focus before taking a mouse click manually
				new = math.Round(math.Clamp((self.mx - 4)*2-1,0,255))
				
				if self.mc[0].click == 1 then
					self.mc[0] = {
						click = 2,
						uid = self.uid, //ALWAYS assign self.uid
						data = {
							col = v
						}
					}
				end
			end
			
			if self.mc[0].click == -1 and self.mc[0].uid == self.uid and self.mc[0].data.col == v then //Exit click function
				//ALWAYS perform click actions on release
				new = math.Round(math.Clamp((self.mx - 4)*2-1,0,255))
				self._.save[v] = new
				
				self.mc[0] = { //ALWAYS clear the click table when finished
					click=0,
					uid=nil,
					data=nil,
				}
			end
			
			s[self._.cur][v] = new
			if not new then
				for i,o in pairs({"r","g","b"}) do
					s[self._.cur][o] = self._.save[o]
				end
			end
			
			self:DrawRect(6,yo+2,(s[self._.cur][v]+1)/2,8)
			self:DrawText(s[self._.cur][v],"Trebuchet18",138,yo-2)
			
			i = i + 1
		end
		
		self:DrawWindowPost()
	end
}
























































